<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/5/14
 * Time: 14:55
 */
namespace app\home\controller;
use think\Db;
class Economic extends Base {
    public function index(){
        if(!session('?my_location_city_id')){
            $shi = 1002;
        }else{
            $shi = session('my_location_city_id');
        }
        $data = Db::table('up_u_user')
            ->where('user_type',2)
            ->where('user_shi',$shi)
            ->where('user_status',1)
            ->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    /**
     * 经纪人详情
     */
    public function getDesignInfo()
    {
        extract(input());
        $user_id = session('user_id');
        $field = 'user_id,user_username,user_shop,user_image,user_phone,user_year_3,user_cases_3,user_signs_3,user_content_3';
        $field .= ',user_chengjiao,user_fuwu,user_fen,user_ercode';
        $data = Db::table('up_u_user')
            ->where('user_id',$uid)
            ->field($field)
            ->find();
        if($data['user_signs_3']){
            $data['user_signs_3'] = explode(',',$data['user_signs_3']);
        }else{
            $data['user_signs_3'] = [];
//            $data['user_signs_3'] = [$data['user_year_3'] . '年经验'];
        }
        $have = Db::table('up_like_design')
            ->where('like_user_id',$user_id)
            ->where('like_user_id_3',$uid)
            ->find ();
        $like = 0;
        if($have){
            $like = 1;//已关注
        }
        //
        //---1、最近买卖成交
        $maihouse = Db::table('up_house h')
            ->where('h_user_id',$uid)
            ->where('h_type','>',1)
            ->where('h_done',2)
            ->order('h_done_time DESC')
            ->field(['h_id','h_xiaoqu','h_done_time','h_price','h_unit_price','h_shis','h_tings','h_mianji','h_floor'])
            ->page(1,3)
            ->select();
        //---2、最近租赁成交
        $zuhouse = Db::table('up_house h')
            ->where('h_user_id',$uid)
            ->where('h_type',1)
            ->where('h_done',2)
            ->order('h_done_time DESC')
            ->field(['h_id','h_xiaoqu','h_done_time','h_price','h_unit_price','h_shis','h_tings','h_mianji','h_floor'])
            ->page(1,3)
            ->select();
        //---3、客户评价
        $usercomment = Db::table('up_user_comment uc')
            ->join ('up_u_user u','uc.uc_who_id = u.user_id','LEFT')
            ->where('uc_user_id',$uid)
            ->order('uc_time DESC')
            ->field('uc.*,user_username')
            ->page(1,3)
            ->select();
        if($usercomment){
            foreach ($usercomment as $k => $v){
                if($v['uc_signs']){
                    $usercomment[$k]['uc_signs'] = explode(',',$v['uc_signs']);
                }else{
                    $usercomment[$k]['uc_signs'] = [];
                }
            }
        }
        //发布的仨
        $h_type1 = $this->getHousByTypeId($uid,1);
        $h_type2 = $this->getHousByTypeId($uid,2);
        $h_type3 = $this->getHousByTypeId($uid,3);
        $res = ['data'=>$data,'like'=>$like
            ,'maihouse'=>$maihouse
            ,'zuhouse'=>$zuhouse
            ,'usercomment'=>$usercomment
            ,'h_type1'=>$h_type1
            ,'h_type2'=>$h_type2
            ,'h_type3'=>$h_type3
        ];
        $this->assign($res);
        return $this->fetch('economic/detail');

    }
    /**
     * 全部评论
     */
    public function allComm()
    {
        extract(input());
        if(isset($uid) && $uid){
            session('comment_uid',$uid);
        }else{
            $uid = session('comment_uid');
        }
        $usercomment = Db::table('up_user_comment uc')
            ->join ('up_u_user u','uc.uc_who_id = u.user_id','LEFT')
            ->where('uc_user_id',$uid)
            ->order('uc_time DESC')
            ->field('uc.*,user_username')
            ->paginate(10)
            ->each(function($item, $key){
                if($item['uc_signs']){
                    $item['uc_signs'] = explode(',',$item['uc_signs']);
                }else{
                    $item['uc_signs'] = [];
                }
                return $item;
            });

        $res = ['data'=>$usercomment,'uid'=>$uid];
        $this->assign($res);
        return $this->fetch('economic/allcomm');

    }

    /**
     * 去评价
     * $uid
     */
    public function gocomm(){
        if(request()->isPost ()){
            extract(input());
            $signs = trim($signs,',');
            $data = [
                'uc_user_id' => $uid,
                'uc_who_id' => session('user_id'),
                'uc_star' => $star,
                'uc_time' => date('Y-m-d H:i:s'),
                'uc_signs' => $signs
            ];
            $res = Db::table('up_user_comment')->insert($data);
            if($res){
                $all_num = Db::table('up_user_comment')->where('uc_user_id',$uid)->count();
                $all_star = Db::table('up_user_comment')->where('uc_user_id',$uid)->sum('uc_star');
                $fen = round($all_star/$all_num,1);
                Db::table('up_u_user')->where('user_id',$uid)->setField('user_fen',$fen);
                return json(['code'=>200,'msg'=>'发布成功']);
            }else{
                return json(['code'=>100,'msg'=>'发布失败，请刷新重试']);
            }
        }
        $uid = input('uid');
        $c_ss = Db::table('up_user_comment_sign')->select();
        $this->assign('uid',$uid);
        $this->assign('c_ss',$c_ss);
        return $this->fetch();
    }
    protected function getHousByTypeId($uid,$type)
    {
        $res = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where('h_user_id',$uid)
            ->where('h_status',1)
            ->where('h_type',$type)
            ->field(['h_id','h_title','h_hot_title','h_done','h_done_time','h_xiaoqu','h_price','h_shi','h_type','h_shis','h_tings','h_mianji','h_img','h_weis','district','h_use_top_status','h_use_fre_status','h_use_top_time','h_use_fre_time'])
            ->order('h_time DESC')
            ->page(1,3)
            ->select();
        return $res;
    }
    /**
     * 获取聊天信息：从列表进或者从经纪人出聊天进或者从消息列表处进
     * 传参：user_id和user_id_other和page
     * 只要m_one_user_id==user_id，就说明是这条消息是我的，否则是对方的，头像都取这个，只是位置不同
     */
    public function getNewsRecForPc()
    {
        extract(input());
        if(!session('?user_id')){
            return json(['status'=>404,'msg'=>'请登录！']);
        }
        $user_id = session('user_id');
        if($user_id == $user_id_other){
            return json(['status'=>100,'msg'=>'不能自己和自己聊']);
        }
        //m_one_user_id等于user_id就是我发的
        $data = Db::table('up_message')->alias('m')
            ->join('up_u_user u','m.m_one_user_id=u.user_id','LEFT')
            ->where (function ($query) use ($user_id,$user_id_other){
                $query->where('m_one_user_id',$user_id)->where('m_reply_user_id',$user_id_other);
            })
            ->whereOr(function ($query) use ($user_id,$user_id_other){
                $query->where('m_reply_user_id',$user_id)->where('m_one_user_id',$user_id_other);
            })
            ->order('m_time DESC')
            ->field('user_image,m_content,m_time,m_one_user_id,m_reply_user_id')
            ->page($page,120)
            ->select();
//        ---------------------所有和此人的消息改状态start---------------------
        Db::table('up_message')
            ->where('m_reply_user_id',$user_id)
            ->where('m_one_user_id',$user_id_other)
            ->where('m_read','notlike',"%,$user_id,%")
            ->update(['m_read'=>",$user_id,$user_id_other,"]);
//        ---------------------所有和此人的消息改状态 end----------------------
        $data = array_reverse($data);
        $who = Db::table('up_u_user')->where('user_id',$user_id_other)->value('user_username');
        return json(['status'=>200,'msg'=>$data,'who'=>$who,'user_id'=>$user_id]);
    }
    /**
     * 发聊天信息
     * 传参：user_id和user_id_other和$m_content
     * 只要m_one_user_id==user_id，就说明是这条消息是我的，否则是对方的，头像都取这个，只是位置不同
     */
    public function startChatsForPC()
    {
        extract(input());
        $user_id = session('user_id');
        $have = Db::table('up_message')
            ->where (function ($query) use ($user_id,$user_id_other){
                $query->where('m_one_user_id',$user_id)->where('m_reply_user_id',$user_id_other);
            })
            ->whereOr(function ($query) use ($user_id,$user_id_other){
                $query->where('m_reply_user_id',$user_id)->where('m_one_user_id',$user_id_other);
            })
            ->find();
        $data = array();
        $data['m_one_user_id'] = $user_id;
        $data['m_reply_user_id'] = $user_id_other;
        $data['m_content'] = $m_content;
        $data['m_time'] = date('Y-m-d H:i:s');
        $data['m_start'] = $user_id;
        $data['m_end'] = $user_id_other;
        $data['m_read'] = ',' . $user_id . ',';
        if($have){
            $data['m_start'] = $have['m_start'];
            $data['m_end'] = $have['m_end'];
        }
        $res = Db::table('up_message')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'发送成功']);
        }else{
            return json(['status'=>0,'msg'=>'发送失败']);
        }
    }
}